home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / Library / Implementation / HTList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  2.6 KB  |  127 lines

  1. /*    A small List class                          HTList.c
  2. **    ==================
  3. **
  4. **    A list is represented as a sequence of linked nodes of type HTList.
  5. **    The first node is a header which contains no object.
  6. **    New nodes are inserted between the header and the rest of the list.
  7. */
  8.  
  9. #include "HTList.h"
  10.  
  11. #include <stdio.h>                /* joe@athena, TBL 921019 */
  12.  
  13. HTList * HTList_new NOARGS
  14. {
  15.   HTList *newList = (HTList *)malloc (sizeof (HTList));
  16.   if (newList == NULL) outofmem(__FILE__, "HTList_new");
  17.   newList->object = NULL;
  18.   newList->next = NULL;
  19.   return newList;
  20. }
  21.  
  22. void HTList_delete ARGS1(HTList *,me)
  23. {
  24.   HTList *current;
  25.   while (current = me) {
  26.     me = me->next;
  27.     free (current);
  28.   }
  29. }
  30.  
  31. void HTList_addObject ARGS2(HTList *,me, void *,newObject)
  32. {
  33.   if (me) {
  34.     HTList *newNode = (HTList *)malloc (sizeof (HTList));
  35.     if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
  36.     newNode->object = newObject;
  37.     newNode->next = me->next;
  38.     me->next = newNode;
  39.   }
  40.   else
  41.     if (TRACE) fprintf(stderr,
  42.         "HTList: Trying to add object %p to a nonexisting list\n",
  43.                newObject);
  44. }
  45.  
  46. BOOL HTList_removeObject ARGS2(HTList *,me, void *,oldObject)
  47. {
  48.   if (me) {
  49.     HTList *previous;
  50.     while (me->next) {
  51.       previous = me;
  52.       me = me->next;
  53.       if (me->object == oldObject) {
  54.     previous->next = me->next;
  55.     free (me);
  56.     return YES;  /* Success */
  57.       }
  58.     }
  59.   }
  60.   return NO;  /* object not found or NULL list */
  61. }
  62.  
  63. void * HTList_removeLastObject ARGS1 (HTList *,me)
  64. {
  65.   if (me && me->next) {
  66.     HTList *lastNode = me->next;
  67.     void * lastObject = lastNode->object;
  68.     me->next = lastNode->next;
  69.     free (lastNode);
  70.     return lastObject;
  71.   } else  /* Empty list */
  72.     return NULL;
  73. }
  74.  
  75. void * HTList_removeFirstObject ARGS1 (HTList *,me)
  76. {
  77.   if (me && me->next) {
  78.     HTList * prevNode;
  79.     void *firstObject;
  80.     while (me->next) {
  81.       prevNode = me;
  82.       me = me->next;
  83.     }
  84.     firstObject = me->object;
  85.     prevNode->next = NULL;
  86.     free (me);
  87.     return firstObject;
  88.   } else  /* Empty list */
  89.     return NULL;
  90. }
  91.  
  92. int HTList_count ARGS1 (HTList *,me)
  93. {
  94.   int count = 0;
  95.   if (me)
  96.     while (me = me->next)
  97.       count++;
  98.   return count;
  99. }
  100.  
  101. int HTList_indexOf ARGS2(HTList *,me, void *,object)
  102. {
  103.   if (me) {
  104.     int position = 0;
  105.     while (me = me->next) {
  106.       if (me->object == object)
  107.     return position;
  108.       position++;
  109.     }
  110.   }
  111.   return -1;  /* Object not in the list */
  112. }
  113.  
  114. void * HTList_objectAt ARGS2 (HTList *,me, int,position)
  115. {
  116.   if (position < 0)
  117.     return NULL;
  118.   if (me) {
  119.     while (me = me->next) {
  120.       if (position == 0)
  121.     return me->object;
  122.       position--;
  123.     }
  124.   }
  125.   return NULL;  /* Reached the end of the list */
  126. }
  127.